/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.MyConnectUnit;
import DTO.LoanDetail;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author Quan_Nuce
 */
public class BLoanDetail {
    MyConnectUnit connect;
    public BLoanDetail() {
        this.connect = DAL.DAL.getDAL();
}
    
      public HashSet<LoanDetail>getLoanDetails(String condition, String OderBy) throws Exception{
        ResultSet result = this.connect.Select("LoanDetail",condition,OderBy);
        HashSet<LoanDetail> loanDetails = new HashSet<LoanDetail>();
        while(result.next()){
            LoanDetail loanDetail = new LoanDetail();
            loanDetail.setLoanDetailID(result.getInt("LoanDetailID"));
            loanDetail.setCustomerID(result.getInt("CustomerID"));
            loanDetail.setLoanTypeID(result.getInt("LoanTypeID"));
            loanDetail.setDepartmentID(result.getInt("DepartmentID"));
            loanDetail.setAmount(result.getFloat("Amount"));
            loanDetail.setLoanStartDate(result.getString("LoanStartDate"));
            loanDetail.setLoanEndDate(result.getString("LoanEndDate"));
            loanDetail.setPeriodID(result.getInt("PeriodID"));
            loanDetail.setNote(result.getString("Note"));
            
            loanDetails.add(loanDetail);
        }
        return loanDetails;
    }
      public HashSet<LoanDetail>getLoanDetails(String condition) throws Exception{
        return getLoanDetails(condition,null);
    }
  public HashSet<LoanDetail>getLoanDetails() throws Exception{
        return getLoanDetails(null);
    }
    
   public LoanDetail getByID(int id)throws Exception{
        HashSet<LoanDetail>loanDetails = this.getLoanDetails("LoanDetailID = "+ id);
        if(loanDetails.size()>0)
            return loanDetails.toArray(new LoanDetail[loanDetails.size()])[0];
        return null;
       
   }
      public void Insert(LoanDetail loanDetail)throws Exception{
        HashMap<String, Object>map = new HashMap<String,Object>();
        map.put("LoanDetailID", loanDetail.getLoanDetailID());
        map.put("CustomerID", loanDetail.getCustomerID());
        map.put("LoanTypeID", loanDetail.getLoanTypeID());
        map.put("DepartmentID", loanDetail.getDepartmentID());
        map.put("Amount", loanDetail.getAmount());
        map.put("LoanStartDate", loanDetail.getLoanStartDate());
        map.put("LoanEndDate", loanDetail.getLoanEndDate());
        map.put("PeriodID", loanDetail.getPeriodID());
        map.put("Note", loanDetail.getNote());
        this.connect.Insert("LoanDetail ", map);
       
   }
        public void Inserts(HashSet<LoanDetail> loanDetails)throws Exception{
        for(LoanDetail loanDetail:loanDetails)
            this.Insert(loanDetail);
       
   }
          public void Update(LoanDetail loanDetail)throws Exception{
        HashMap<String, Object>map = new HashMap<String,Object>();
         map.put("CustomerID", loanDetail.getCustomerID());
        map.put("LoanTypeID", loanDetail.getLoanTypeID());
        map.put("DepartmentID", loanDetail.getDepartmentID());
        map.put("Amount", loanDetail.getAmount());
        map.put("LoanStartDate", loanDetail.getLoanStartDate());
        map.put("LoanEndDate", loanDetail.getLoanEndDate());
        map.put("PeriodID", loanDetail.getPeriodID());
        map.put("Note", loanDetail.getNote());
      
        this.connect.Update("LoanDetail", map,"LoanDetailID="+loanDetail.getLoanDetailID());
       
   }
           public void Updates(HashSet<LoanDetail> loanDetails)throws Exception{
        for(LoanDetail loanDetail:loanDetails)
            this.Update(loanDetail);
       
   }
   public void Delete(LoanDetail loanDetail) throws Exception{
        this.connect.Delete("LoanDetail", "LoanDetailID="+loanDetail.getLoanDetailID());
    }
       public void Deletes(HashSet<LoanDetail> loanDetails) throws Exception {
        for(LoanDetail loanDetail:loanDetails)
            this.Delete(loanDetail);
   }
}
